//
//  XFHomeViewController.swift
//  mxfweibo
//
//  Created by apple1 on 15/12/15.
//  Copyright © 2015年 莫祥富. All rights reserved.
//

import UIKit

enum XFStatusCellReuseIdentifier: String {
    case StatusNormalCell = "StatusNormalCell"  // 原创cell的标识
    case StatusForwardCell = "StatusForwardCell"    // 转发cell标识
}


class XFHomeViewController: XFBaseViewController {

 
    var statuses : [XFStatus]? {
        didSet {
            self.tableView.reloadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !XFUserAccount.userLogin {
            return
        }
        
        setupNav()
        
        //注册转发cell
        tableView.registerClass(XFStatusForwardCell.self, forCellReuseIdentifier: XFStatusCellReuseIdentifier.StatusForwardCell.rawValue)
        
        //注册原创cell
        tableView.registerClass(XFStatusNormalCell.self, forCellReuseIdentifier: XFStatusCellReuseIdentifier.StatusNormalCell.rawValue)
        
        tableView.estimatedRowHeight = 200
        
//        tableView.rowHeight = UITableViewAutomaticDimension
        
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        
        XFStatus.xxLoadStatus { (statuses, error) -> () in
            if error != nil {
                return
            }
            
            self.statuses = statuses
            
//            self.tableView.reloadData()
        }
    }

    //创建导航栏
    private func setupNav() {
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch")
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop")
        
        let title = XFUserAccount.loadUserAccount()?.screen_name ?? "没有获取用户名"
        let titleBtn = XFHomeTitleView(imageName: "navigationbar_arrow_down", title: title)
        
        titleBtn.addTarget(self, action: "homeTitleBtnClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        navigationItem.titleView = titleBtn
    }
    
    //标题按钮点击事件
    func homeTitleBtnClick(button:UIButton) {
        
        button.selected = !button.selected
        
        UIView.animateWithDuration(0.25) { () -> Void in
            button.imageView?.transform = button.selected ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.001)) : CGAffineTransformIdentity
        }
        
    }
    
    //MARK: - tableView数据源方法 代理方法
    //显示数据条数
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let status = statuses![indexPath.row]
        
        let cellID = status.cellID()
        
        let cell = tableView.dequeueReusableCellWithIdentifier(cellID) as! XFStatusCell
        
        
        cell.status = status
        
        return cell
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        let status = self.statuses![indexPath.row]
        
        if status.rowHeight != nil {
            
            return status.rowHeight!
        }
        
        let cellID = status.cellID()
        
        let cell = tableView.dequeueReusableCellWithIdentifier(cellID) as! XFStatusCell
        
        let rowHeight = cell.rowHeight(status)
        
        status.rowHeight = rowHeight
        
        return rowHeight
    }
    
}
